package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        if (setmealPageQueryDTO.getCategoryId() !=null || setmealPageQueryDTO.getName()!=null || setmealPageQueryDTO.getStatus()!=null){
            PageHelper.startPage(1,setmealPageQueryDTO.getPageSize());
        }else {
            PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        }
        Page<SetmealVO>  page= setmealMapper.queryPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmea(setmeal);
        //在mapper里面执行sql语句后，将id写入到setmeal中了
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO=setmealMapper.getById(id);
        List<SetmealDish> setmealDishs=setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }

    @Override
    @Transactional
    public void ChangeSetmal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.change(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<Long> setmealIds = new ArrayList<>();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealIds.add(setmealDish.getSetmealId());
        });
        if (setmealDishes.size()>0){
            setmealDishMapper.delete(setmealIds);
        }
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status,Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.change(setmeal);
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        Integer status=setmealMapper.getByIds(ids);
        if (status>0){
            throw new DeletionNotAllowedException("起售中的套餐不能删除");
        }
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }
}
